const app = getApp()
import util from '../../../utils/util'
import {
  GetVisit
} from '../../../config/api'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    grider_storge: '',
    page: 1,
    limit: 10,
    lists: [],
    isEmpty: false, // 是否为空
    isBottom: false, // 是否无数据触底了
    isReachBtm: false, // 是否触发上拉加载
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let grider_storge = wx.getStorageSync('grider')
    this.setData({
      grider_storge
    })
    this.getList(); // 获取列表
  },

  getList() { // 获取列表
    let that = this;
    let params = {
      uid: that.data.grider_storge.id,
      vollege_id: app.globalData.vollege_id,
      page: this.data.page,
      limit: this.data.limit
    }
    util.apiRequest(GetVisit, params, null).then(r => {
      console.log('走访列表', r)
      var newList = that.data.lists.concat(r.data.data)
      if (r.data.data.length == 0) {
        wx.showToast({
          title: '暂无更多',
          icon: 'none',
        })
      } 
      that.setData({
        lists: newList
      })

      wx.hideLoading();

      if (newList.length == 0) {
        that.setData({
          isEmpty: true
        })
      } else {
        that.setData({
          isEmpty: false
        })
      }

    })
  },

  startVisit(e) { // 开始走访
    console.log(e)
    wx.navigateTo({
      url: '/pages/grid/visit/visitForm/visitForm',
    })
  },

  toVisitDetail(e) { // 跳转详情
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/grid/visit/visitDetail/visitDetail?id=' + id,
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    var that = this;
    var page = this.data.page;
    page++;
    that.setData({
      page: page
    })
    console.log('page++', page)
    this.getList();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})